# 导包
from api.order import OrderAPI
from common.utils import common_assert


# 创建测试类
class TestOrderAPI:
    # 初始化属性
    token = None
    goods_id = None

    # 类前置方法
    def setup_class(self):
        # 实例化接口类
        self.order_api = OrderAPI()

    # 登录
    def test01_login(self):
        response = self.order_api.login(username="zj010128", password="a123456")
        # 提取token
        TestOrderAPI.token = response.json().get("data").get("token")
        print(TestOrderAPI.token)
        # 断言
        common_assert(response)

    # 搜索
    def test02_search(self):
        response = self.order_api.search(keyword="母亲节")
        TestOrderAPI.goods_id = response.json().get("data").get("list")[0].get("id")
        print(TestOrderAPI.goods_id)
        common_assert(response)

    # 添加购物车
    def test03_add_cart(self):
        add_data = {"goodsId": TestOrderAPI.goods_id, "number": 5, "productId": 2}
        response = self.order_api.add_cart(TestOrderAPI.token, add_data)
        print(response.json())
        common_assert(response)

    # 提交订单
    def test04_submit_order(self):
        test_data = {"addressId": "2", "cartId": "1", "couponId": "0", "userCouponId": "0", "grouponLinkId": 0,
                     "grouponRulesId": 0, "message": ""}
        response = self.order_api.submit_order(TestOrderAPI.token, test_data)
        print(response.json())
        common_assert(response)
